// 内容相关工具函数

import { ContentType } from '../types';

// 支持的图片格式
export const SUPPORTED_IMAGE_FORMATS = ['jpg', 'jpeg', 'png', 'gif', 'webp'];

// 支持的视频格式
export const SUPPORTED_VIDEO_FORMATS = ['mp4', 'avi', 'mov', 'webm'];

// 文件大小限制（字节）
export const MAX_IMAGE_SIZE = 5 * 1024 * 1024; // 5MB
export const MAX_VIDEO_SIZE = 100 * 1024 * 1024; // 100MB
export const MAX_COVER_SIZE = 5 * 1024 * 1024; // 5MB

// 图片数量限制
export const MAX_IMAGES_COUNT = 8;

// 获取文件扩展名
export const getFileExtension = (filename: string): string => {
  return filename.split('.').pop()?.toLowerCase() || '';
};

// 验证图片文件
export const validateImageFile = (file: File): { valid: boolean; error?: string } => {
  const extension = getFileExtension(file.name);
  
  if (!SUPPORTED_IMAGE_FORMATS.includes(extension)) {
    return {
      valid: false,
      error: `不支持的图片格式，支持的格式：${SUPPORTED_IMAGE_FORMATS.join(', ')}`
    };
  }
  
  if (file.size > MAX_IMAGE_SIZE) {
    return {
      valid: false,
      error: `图片文件大小不能超过${MAX_IMAGE_SIZE / 1024 / 1024}MB`
    };
  }
  
  return { valid: true };
};

// 验证视频文件
export const validateVideoFile = (file: File): { valid: boolean; error?: string } => {
  const extension = getFileExtension(file.name);
  
  if (!SUPPORTED_VIDEO_FORMATS.includes(extension)) {
    return {
      valid: false,
      error: `不支持的视频格式，支持的格式：${SUPPORTED_VIDEO_FORMATS.join(', ')}`
    };
  }
  
  if (file.size > MAX_VIDEO_SIZE) {
    return {
      valid: false,
      error: `视频文件大小不能超过${MAX_VIDEO_SIZE / 1024 / 1024}MB`
    };
  }
  
  return { valid: true };
};

// 验证封面文件
export const validateCoverFile = (file: File): { valid: boolean; error?: string } => {
  const extension = getFileExtension(file.name);
  
  if (!SUPPORTED_IMAGE_FORMATS.includes(extension)) {
    return {
      valid: false,
      error: `不支持的封面格式，支持的格式：${SUPPORTED_IMAGE_FORMATS.join(', ')}`
    };
  }
  
  if (file.size > MAX_COVER_SIZE) {
    return {
      valid: false,
      error: `封面文件大小不能超过${MAX_COVER_SIZE / 1024 / 1024}MB`
    };
  }
  
  return { valid: true };
};

// 验证创建内容的文件
export const validateCreateContentFiles = (
  contentType: ContentType,
  files: {
    cover?: File;
    images?: File[];
    video?: File;
  }
): { valid: boolean; errors: string[] } => {
  const errors: string[] = [];
  
  // 验证封面
  if (files.cover) {
    const coverValidation = validateCoverFile(files.cover);
    if (!coverValidation.valid && coverValidation.error) {
      errors.push(coverValidation.error);
    }
  }
  
  if (contentType === ContentType.IMAGE) {
    // 图片内容验证
    if (!files.images || files.images.length === 0) {
      errors.push('图片内容至少需要上传一张图片');
    } else {
      if (files.images.length > MAX_IMAGES_COUNT) {
        errors.push(`图片数量不能超过${MAX_IMAGES_COUNT}张`);
      }
      
      files.images.forEach((image, index) => {
        const imageValidation = validateImageFile(image);
        if (!imageValidation.valid && imageValidation.error) {
          errors.push(`第${index + 1}张图片：${imageValidation.error}`);
        }
      });
    }
  } else if (contentType === ContentType.VIDEO) {
    // 视频内容验证
    if (!files.video) {
      errors.push('视频内容必须上传视频文件');
    } else {
      const videoValidation = validateVideoFile(files.video);
      if (!videoValidation.valid && videoValidation.error) {
        errors.push(videoValidation.error);
      }
    }
  }
  
  return {
    valid: errors.length === 0,
    errors
  };
};

// 格式化文件大小
export const formatFileSize = (bytes: number): string => {
  if (bytes === 0) return '0 B';
  
  const k = 1024;
  const sizes = ['B', 'KB', 'MB', 'GB'];
  const i = Math.floor(Math.log(bytes) / Math.log(k));
  
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
};

// 生成内容类型显示文本
export const getContentTypeText = (contentType: ContentType): string => {
  switch (contentType) {
    case ContentType.IMAGE:
      return '图片';
    case ContentType.VIDEO:
      return '视频';
    default:
      return '未知';
  }
};

// 生成上传状态显示文本
export const getUploadStatusText = (status: string): string => {
  switch (status) {
    case 'PENDING':
      return '等待处理';
    case 'UPLOADING':
      return '上传中';
    case 'UPDATING':
      return '更新中';
    case 'COMPLETED':
      return '已完成';
    case 'FAILED':
      return '失败';
    default:
      return '未知状态';
  }
};

// 生成操作类型显示文本
export const getOperationTypeText = (operationType: string): string => {
  switch (operationType) {
    case 'UPLOAD_IMAGES':
      return '上传图片';
    case 'UPLOAD_VIDEO':
      return '上传视频';
    case 'UPLOAD_COVER':
      return '上传封面';
    case 'UPDATE_IMAGES':
      return '更新图片';
    case 'UPDATE_VIDEO':
      return '更新视频';
    case 'UPDATE_COVER':
      return '更新封面';
    default:
      return '未知操作';
  }
};

// 检查内容是否可以编辑
export const canEditContent = (uploadStatus: string): boolean => {
  return uploadStatus === 'COMPLETED' || uploadStatus === 'FAILED';
};

// 检查内容是否正在处理中
export const isContentProcessing = (uploadStatus: string): boolean => {
  return uploadStatus === 'PENDING' || uploadStatus === 'UPLOADING' || uploadStatus === 'UPDATING';
}; 